<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.entity.StringEntity" %>
<%@ page import="org.apache.http.impl.client.CloseableHttpClient" %>
<%@ page import="org.apache.http.impl.client.HttpClients" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.suntai.utils.HttpUtil" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%

    String requestid = request.getParameter( "requestid" );
    String tablename = "formtable_main_204";
    out.print( ">>>>>>WeaSalesContractAction>>>>>" + requestid+"<br/>" );
    String resultText = "";
    try {
        rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
        rs.next();
        JSONObject jsonData = new JSONObject();
        //企业编号
        jsonData.put( "companyCode", Util.null2String( rs.getString( "qybh" ) ));
        //合同编号
        jsonData.put( "contractCode", Util.null2String( rs.getString( "htbh" ) ) );
        //收入金额
        jsonData.put( "incomeAmount", Util.null2String( rs.getString( "srje" ) ) );
        //收入日期
        jsonData.put( "incomeDate", Util.null2String( rs.getString( "kprq" ) ) );
        //创建人
        jsonData.put( "createBy", Util.null2String( rs.getString( "sqrdxwb" ) ) );
        //租户编码
        jsonData.put( "tenantCode", Util.null2String( rs.getString( "zhbm" ) ) );
        //状态
        jsonData.put( "status", "rkz" );
        out.print( ">>>>>>jsonData>>>>>" + jsonData.toJSONString() + "<br/>" );
        resultText = addIncom( jsonData.toJSONString() );
        out.print( ">>>>>>resultText>>>>>" + resultText + "<br/>" );
        JSONObject resultJson = JSONObject.parseObject( resultText );
        String code = Util.null2String( resultJson.getString( "code" ) );
        String data = Util.null2String( resultJson.getString( "data" ) );
        out.print( "code=" + code + ",data=" + data );
    } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace();
    }

%>
<%!

    public static final String URL = "http://10.119.49.22:8080/LesCont/";
    public static final String KEY = "20dd1883e16c4a1bb4e248a278bdeead";
    public static final String Token = "Token";
    public static final String Sellcont = "Sellcont";
    public static final String BatchSellcont = "BatchSellcont";
    public static final String ChangeSellcont = "ChangeSellcont";
    public static final String BatchChangeSellcont = "BatchChangeSellcont";
    public static final String Paycont = "Paycont";
    public static final String BatchPaycont = "BatchPaycont";
    public static final String Income = "Income";
    public static final String Proceeds = "Proceeds";

    private static Map map = new HashMap();
    static {
        map.put(Token, "token/getToken");
        map.put(Sellcont, "api/sellcont/insert");
        map.put(BatchSellcont, "api/sellcont/insertList");
        map.put(ChangeSellcont, "api/change/insert");
        map.put(BatchChangeSellcont, "api/change/insertList");
        map.put(Paycont, "api/paycont/insert");
        map.put(BatchPaycont, "api/paycont/insertList");
        map.put(Income, "api/income/insert");
        map.put(Proceeds, "api/proceeds/insert");
    }
    public static String getToken() {
        String url = URL+map.get("Token");
        Map<String, String> param = new HashMap<>();
        param.put( "key", KEY );
        String resultText = HttpUtil.httpGet( url, param );
        JSONObject resultJson = JSONObject.parseObject(resultText);
        String data = Util.null2String(resultJson.getString( "data" ));
        return data;
    }
    public static String Invoke(String type,String params){
        BaseBean baseBean = new BaseBean();
        baseBean.getPropValue("","");
        String url = URL+map.get(type);
        Map<String, String> header = new HashMap<>();
        header.put( "ContentType", "application/json" );
        String token = getToken();
        header.put( "token", token );
        String ecode = "UTF-8";
        return httpPostRaw( url, params, header, ecode );
    }
    public static String addIncom(String params) {
        return Invoke(Income, params);
    }
    /**
     * 发送 http post 请求，参数以原生字符串进行提交
     *
     * @param url
     * @param encode
     * @return
     */
    public static String httpPostRaw(String url, String stringJson, Map<String, String> headers, String encode) {
        if (encode == null) {
            encode = "utf-8";
        }
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {

            //HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost( url );

            //设置header
            httpost.setHeader( "Content-type", "application/json" );
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader( entry.getKey(), entry.getValue() );
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity( stringJson, encode );
            httpost.setEntity( stringEntity );


            //响应信息
            httpResponse = closeableHttpClient.execute( httpost );
            HttpEntity entity = httpResponse.getEntity();
            content = EntityUtils.toString( entity, encode );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
%>